package ex_from_web;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Reversing an array order in Java example
 * 1.Reversing an array of objects
 * 2.Reversing an array of primitive types
 */
public class reverse_array  {

    public static void main(String[] args) {
        String[] words = {
                "Cat", 
                "Table",
                "Table",
                "Letter", 
                "World"};
        
        /*
            Array of objects
            Use Collections.reverse() method 
         */
        System.out.println("Reversed array of objects:");
        List<String> list = Arrays.asList(words);
        Collections.reverse(list);
        words = (String[]) list.toArray();
        for(String s : words) {
            System.out.println(s);
        }
        
        /*
            Array of primitive types
            Replace elements to get a elements in reverse order
        */
        System.out.println("Reversed array of primitive types:");
        int[] array = {
                1, 
                2, 
                3,
                3,
                4, 
                5};
        for (int i=0; i<(int)(array.length/2); i++) {
            int temp = array[i];
            array[i] = array[array.length - i - 1];
            array[array.length - i - 1] = temp;
        }
        for (int i : array) {
            System.out.println(i);
        }
    }

}